local skel = fk.CreateSkill{
  name = "kqf__zhilian",
}

Fk:loadTranslationTable{
  ["kqf__zhilian"] = "枝连",
  [":kqf__zhilian"] = "每回合限一次，当有角色拼点后，若你本回合获得过牌或/且失去过牌，拼点赢的角色可以对你和/或没赢的角色造成1点冰冻伤害；",

  ["#kqf__zhilian1_ask"] = "枝连：你可以对 %src 造成1点冰冻伤害",
  ["#kqf__zhilian2_ask"] = "枝连：你可以对 %src 和 %dest 各造成1点冰冻伤害",
  ["#kqf__zhilian3_ask"] = "枝连：你可以对 %src 或 %dest 造成1点冰冻伤害",

  ["$kqf__zhilian1"] = "花落水自流，何须怨东风。",
  ["$kqf__zhilian2"] = "请勿葬我身于黄土，可置此魂于春风。",
}


skel:addEffect(fk.PindianFinished, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return (player:getMark("kqf__zhilian-turn") > 0) and (player:getMark("kqf__zhilian-turn") < 10)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:sendLog({type = "finish事件"})
    if not player:getMark("kqf__zhilianwinid-turn") then return end
    local winner = room:getPlayerById(player:getMark("kqf__zhilianwinid-turn"))
    local lose = room:getPlayerById(player:getMark("kqf__zhilianlosid-turn"))
    if winner.dead then
      return
    end
    if lose.dead then
      lose = player
    end
    local list = {}
    table.insertIfNeed(list,player)
    table.insertIfNeed(list,lose)
    local choose
    if #list == 1 then
      choose = room:askToChoosePlayers(winner,{
        min_num = 1,
        max_num = 1,
        targets = list,
        skill_name = skel.name,
        prompt = "#kqf__zhilian1_ask:" .. list[1].id,
        cancelable = true,
      })
    else
      if player:getMark("kqf__zhilian-turn") > 2 then
        --或
        choose = room:askToChoosePlayers(winner,{
          min_num = 1,
          max_num = 1,
          targets = list,
          skill_name = skel.name,
          prompt = "#kqf__zhilian3_ask:" .. list[1].id ..":".. list[2].id,
          cancelable = true,
        })
      else
        --且
        choose = room:askToChoosePlayers(winner,{
          min_num = 2,
          max_num = 2,
          targets = list,
          skill_name = skel.name,
          prompt = "#kqf__zhilian2_ask:" .. list[1].id ..":".. list[2].id,
          cancelable = true,
        })
      end
    end
    if choose then
      player:broadcastSkillInvoke(skel.name)
      player.room:notifySkillInvoked(player, skel.name)
      room:setPlayerMark(player,"kqf__zhilian-turn",999)
      room:sortByAction(choose)
      for _,p in ipairs(choose)do
        room:damage{
        from = winner,
        to = p,
        damage = 1,
        damageType = fk.IceDamage,
        skillName = skel.name,
      }
      end
    end
  end,
})

skel:addEffect(fk.PindianResultConfirmed, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local winner = data.winner
    room:sendLog({type = "confi事件"})
    if winner then
      if winner == data.from then
        room:setPlayerMark(player,"kqf__zhilianwinid-turn",data.from.id)
        room:setPlayerMark(player,"kqf__zhilianlosid-turn",data.to.id)
      else
        room:setPlayerMark(player,"kqf__zhilianwinid-turn",data.to.id)
        room:setPlayerMark(player,"kqf__zhilianlosid-turn",data.from.id)
      end
    else
      room:setPlayerMark(player,"kqf__zhilianwinid-turn",0)
      room:setPlayerMark(player,"kqf__zhilianlosid-turn",0)
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil or turn_event.data.who.dead then return end
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          event:setCostData(self, {kflag = 1})
          return true
        elseif move.from == player then
          event:setCostData(self, {kflag = 2})
          return true
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local kflag = event:getCostData(self).kflag
    if player:getMark("kqf__zhilian-turn") > 2 then return end
    if not (player:getMark("kqf__zhilian-turn") == kflag) then
      room:addPlayerMark(player,"kqf__zhilian-turn",kflag)
    end
  end,
})

return skel